package boot

import (
	"github.com/gogf/gf/os/gcron"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/os/gsession"
	"go-middle/app/admin/service/cronservice"
	"go-middle/library/token"
	"time"

	"github.com/gogf/gf-swagger/swagger"
	"github.com/gogf/gf/frame/g"
)

func init() {
	c := g.Config()
	s := g.Server()
	// session & token
	token.Redis = g.Redis("session")
	token.SessionStore = gsession.NewStorageRedisHashTable(token.Redis)
	token.SessionTTL = time.Duration(c.GetInt64("server.SessionMaxAge")) * time.Second
	s.SetSessionMaxAge(token.SessionTTL) // 一定要 使配置文件 SessionMaxAge 可设置为秒
	s.SetSessionStorage(token.SessionStore)
	s.SetRewrite("/favicon.ico", "/favicon.ico")
	s.SetAccessLogEnabled(true)
	//api 文档
	if c.GetString("RunModel") != "prod" {
		s.Plugin(&swagger.Swagger{
			Info: swagger.SwaggerInfo{
				Title:          "",
				Version:        "1.0.0",
				TermsOfService: "",
				Description:    "",
			},
			Schemes:       []string{"http", "https"},
			Host:          "",
			BasicPath:     "",
			BasicAuthUser: "admin",
			BasicAuthPass: "admin",
		})
		g.DB().SetDebug(true)
	}
	gcron.SetLogLevel(glog.LEVEL_ALL)
	if c.GetBool("RunCron") {
		cronservice.Restart()
	}
}
